package com.rc.saas.tenant.controller.elec;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.elec.ElecPileOffline;
import com.rc.saas.tenant.model.elec.ElecPileOfflineExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.elec.PileOfflineSearch;
import com.rc.saas.tenant.service.elec.ElecPileOfflineService;
import com.rc.saas.tenant.service.elec.ElecPileService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.elec.ElecPileOfflineResultVo;
import com.rc.saas.tenant.vo.elec.PileOffineStatisticsVo;
import com.rc.saas.tenant.vo.elec.PileOfflineVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2021/10/28
 */
@Controller
@RequestMapping("/pileOffline")
public class PileOfflineController {
    private static Logger logger = LogManager.getLogger(PileOfflineController.class);

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private ElecPileOfflineService elecPileOfflineService;

    /**
     * 跳转离线桩页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String elecPileOfflinePage() {
        return "/templates/elec/pileOffline";
    }

    /**
     * 返回桩列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPagePileOffline", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPagePileOffline(PileOfflineSearch pileOfflineSearch) {
        PageInfo<PileOfflineVo> pageInfo = elecPileService.listPagePileOffline(pileOfflineSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转桩离线详情页面
     * @param elecPileId
     * @param beginTime
     * @param endTime
     * @param model
     * @return
     */
    @RequestMapping(value = "/detailOfflinePile",method = RequestMethod.GET)
    public String detailOfflinePile(Long elecPileId, String beginTime,String endTime, Model model){
        model.addAttribute("elecPileId",elecPileId);
        model.addAttribute("beginTime",beginTime);
        model.addAttribute("endTime",endTime);
        return "/templates/elec/detailPileOffline";
    }

    /**
     * 查询桩离线信息
     * @param elecPileId
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/selectPileOfflineInfo",method = RequestMethod.GET)
    @ResponseBody
    public TResponse selectPileOfflineInfo(Long elecPileId, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime){
        PileOfflineVo pileOfflineVo=elecPileService.selectPileOfflineInfo(elecPileId, beginTime, endTime);
        return TResponse.SUCCESS_DATA(pileOfflineVo);
    }

    /**
     * 返回桩离线详情列表
     * @param pileOfflineSearch
     * @return
     */
    @RequestMapping(value = "/listPagePileOfflineDetail",method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPagePileOfflineDetail(PileOfflineSearch pileOfflineSearch){
        PageInfo<ElecPileOfflineResultVo> pageInfo=elecPileOfflineService.listPagePileOfflineDateil(pileOfflineSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 返回实时离线桩数据
     *
     * @return
     */
    @RequestMapping(value = "/selectPileOffineStatistics", method = RequestMethod.GET)
    @ResponseBody
    public TResponse selectPileOffineStatistics() {
        TenantUser user = ShiroUtils.getSessionUser();
        String tenantCode = user.getTenantCode();
        PileOffineStatisticsVo pileOffineStatisticsVo = elecPileService.selectPileOffineStatistics(tenantCode);
        return TResponse.SUCCESS_DATA(pileOffineStatisticsVo);
    }


    /**
     * 导出实时离线桩列表列表
     */
    @ResponseBody
    @RequestMapping("/exportPileOffline")
    public TResponse exportPileOffline(PileOfflineSearch pileOfflineSearch, HttpServletResponse response) {
        //导出拼接开始条数
        ExportUtils.setDefaultPaging(pileOfflineSearch);
        List<PileOfflineVo> list = elecPileService.listPileOffline(pileOfflineSearch);
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("实时离线桩列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), PileOfflineVo.class).sheet("实时离线桩列表").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出实时离线桩失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    /**
     * 导出离线桩详情列表
     * @param pileOfflineSearch
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exportPileOfflineDetail",method = RequestMethod.GET)
    public TResponse exportPileOfflineDetail(PileOfflineSearch pileOfflineSearch,HttpServletResponse response){
        ExportUtils.setDefaultPaging(pileOfflineSearch);
        List<ElecPileOfflineResultVo> list=elecPileOfflineService.listPileOfflineDetail(pileOfflineSearch);
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("离线桩详情列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ElecPileOfflineResultVo.class).sheet("离线桩详情列表").doWrite(list);
        } catch (Exception e) {
            logger.error("导出离线桩详情列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
        return TResponse.SUCCESS();
    }
}
